package server;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chengzhj
 * @data 2021/9/16 11:28 下午
 */
@Data
public class Mapper {

    public Host host;

    public static class Host {

        private String name; // localhost
        private String appBase; // 部署目录的绝对路径
        public List<Context> contextList;

        public Host(String name, String appBase) {
            this.name = name;
            this.appBase = appBase;
            this.contextList = new ArrayList<>();
        }
        public void addContext(String contextName, String uri, HttpServlet httpServlet) {
            Context context = new Context(contextName, uri, httpServlet);
            contextList.add(context);
        }

        public List<Context> getContextList() {
            return contextList;
        }

        public void setContextList(List<Context> contextList) {
            this.contextList = contextList;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAppBase() {
            return appBase;
        }

        public void setAppBase(String appBase) {
            this.appBase = appBase;
        }
    }

    public static class Context {
        private String contextName; // demo1/demo2
        public Wrapper wrapper;

        public Context(String contextName, String uri, HttpServlet httpServlet) {
            this.contextName = contextName;
            this.wrapper = new Wrapper(uri, httpServlet);
        }

        public String getContextName() {
            return contextName;
        }

        public void setContextName(String contextName) {
            this.contextName = contextName;
        }

        public Wrapper getWrapper() {
            return wrapper;
        }

        public void setWrapper(Wrapper wrapper) {
            this.wrapper = wrapper;
        }
    }

    public static class Wrapper {
        public String uri; // /lagou
        public HttpServlet httpServlet;

        public Wrapper(String uri, HttpServlet httpServlet) {
            this.uri = uri;
            this.httpServlet = httpServlet;
        }

        public String getUri() {
            return uri;
        }

        public void setUri(String uri) {
            this.uri = uri;
        }

        public HttpServlet getHttpServlet() {
            return httpServlet;
        }

        public void setHttpServlet(HttpServlet httpServlet) {
            this.httpServlet = httpServlet;
        }
    }

    // 根据hostName和contextName找到HTTPServlet
    public HttpServlet findByHostContext(String name, String contextName) {
        if (!name.equalsIgnoreCase(host.name)) {
            return null;
        }
        String[] split = contextName.split("/"); // 比如传过来  /demo1/lagou   分割后数组为 "" "demo" "lagou"  三个元素
        contextName = split[1]; // 假设context只有一段 /demo1
        String uri = split[split.length - 1]; // 假设最后一段为uri
        for (Context context : host.getContextList()) {
            // 判断访问的context在mapper是否存在
            if (context.getContextName().equalsIgnoreCase(contextName)) {
                // 判断context中是否有该wrapper
                if (context.getWrapper().getUri().equalsIgnoreCase(uri)) {
                    return context.getWrapper().getHttpServlet();
                }
            }
        }
        return null;
    }
}
